using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using YooAsset;

namespace DL
{
    public class Load : Singleton<Load>
    {
        ResourcePackage package;
        //加载场景 音效  预制体  特效  界面 ... 
        
        protected override void Construct()
        {
            base.Construct();
            YooAssets.Initialize();
            package = YooAssets.CreatePackage("DefaultPackage");
            YooAssets.SetDefaultPackage(package);
            
            StartCoroutine(InitializeYooAsset());
        }
        
        protected override void Release()
        {
            package.UnloadUnusedAssets();
        }

        private IEnumerator InitializeYooAsset()
        {
            var initParameters = new EditorSimulateModeParameters();
            initParameters.SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild("BuiltinBuildPipeline", "DefaultPackage");
            var initOperation = package.InitializeAsync(initParameters);
            yield return initOperation;
            
            if(initOperation.Status == EOperationStatus.Succeed)
                Debug.Log("资源包初始化成功！");
            else 
                Debug.LogError($"资源包初始化失败：{initOperation.Error}");
        }

        
        public Image LoadImage(string imgName)
        {
            AssetHandle handle = YooAssets.LoadAssetSync<Image>("Image_" + imgName);
            return handle.AssetObject as Image;
        }
        
        public Sprite LoadSprite(string imgName)
        {
            AssetHandle handle = YooAssets.LoadAssetSync<Sprite>("Sprite_" + imgName);
            return handle.AssetObject as Sprite;
        }
        
        public GameObject LoadUIPage(string pageName)
        {
            AssetHandle handle = YooAssets.LoadAssetSync<GameObject>("UIPage_" + pageName);
            return handle.AssetObject as GameObject;
            // GameObject uiPage = handle.AssetObject as GameObject;
            // return GameObject.Instantiate(uiPage);
        }

    }
}
